/*
 * @Author: “ygnnli” “1585923072@qq.com”
 * @Date: 2024-07-30 11:33:44
 * @LastEditors: “ygnnli” “1585923072@qq.com”
 * @LastEditTime: 2024-08-10 17:42:53
 * @FilePath: /110/30/shm_ipc/ShmClient.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "Comm.hpp"
#include "Fifo.hpp"
#include <unistd.h>

int main()
{
    key_t key = GetShmKeyOrDie(); //创建一个共享内存
    std::cout << "key: " << ToHex(key) << std::endl;//转换为16进制 打印
    // sleep(2);

    int shmid = GetShm(key, defaultsize);//获取共享内存
    std::cout << "shmid: " << shmid << std::endl;
    // sleep(2);

    char *addr = (char *)ShmAttach(shmid);//将共享内存映射到进程的地址空间
    std::cout << "Attach shm success, addr: " << ToHex((uint64_t)addr) << std::endl;
    // sleep(5);

    memset(addr, 0, defaultsize);//将共享内存清空
    Sync syn;
    syn.OpenWriteOrDie();//打开同步对象

    // 可以进行通信了
    for (char c = 'A'; c <= 'Z'; c++) // pipe, fifo, ->read/write->系统调用， shm -> 没有使用系统调用！！
    {
        addr[c - 'A'] = c;
        sleep(1);
        syn.Wakeup();
    }

    ShmDetach(addr);
    std::cout << "Detach shm success, addr: " << ToHex((uint64_t)addr) << std::endl;
    sleep(5);

    return 0;
}